/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.AttributeTypeHome;
import com.iti.jets.gpts.home.TranServiceTypeAttributeHome;
import com.iti.jets.gpts.home.TranServiceTypeHome;
import com.iti.jets.gpts.pojo.AttributeType;
import com.iti.jets.gpts.pojo.TranServiceType;
import com.iti.jets.gpts.pojo.TranServiceTypeAttribute;
import com.iti.jets.gpts.pojo.TranServiceTypeAttributeId;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

/**
 *
 * @author noha
 */
@Path("/transervicetype")
public class TransportationServiceTypeService {

    /* static SessionFactory sessionFactory = new Configuration().configure()
     .buildSessionFactory();
     */
    TranServiceTypeHome tsth;
    TranServiceTypeAttributeHome transTypeAttDAO;
    AttributeTypeHome dao;

    public TransportationServiceTypeService() {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        tsth = new TranServiceTypeHome(sessionFactory);
        transTypeAttDAO = new TranServiceTypeAttributeHome(sessionFactory);
        dao = new AttributeTypeHome(sessionFactory);
    }

    @Path("/getalltranservicetype/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public JSONArray getTranServiceTypeIndex(@PathParam(value = "id") int id) {
        //  public static void main(String[] args) throws JSONException {
        //   int id = 1;
        JSONArray jsonAttributesArr = new JSONArray();

        // TranServiceTypeHome tsth = new TranServiceTypeHome(sessionFactory);
        TranServiceType transportationServiceTypeObject = tsth.read(id);
        Hibernate.initialize(transportationServiceTypeObject);
        JSONObject jsontranservcetype = new JSONObject();
        try {
            jsontranservcetype.put("size", transportationServiceTypeObject.getTranServiceTypeAttributes().size());
            int k = 0;
            for (TranServiceTypeAttribute attribute : (Set<TranServiceTypeAttribute>) transportationServiceTypeObject.getTranServiceTypeAttributes()) {
                System.out.println("name" + attribute.getAttributeType().getName());
                JSONObject tstA = new JSONObject();
                tstA.put("attributetypevalue" + k, attribute.getValue());
                tstA.put("attributetypename" + k, attribute.getAttributeType().getName());
                tstA.put("attributetypeid" + k, attribute.getAttributeType().getId());
                tstA.put("attributetypeisnumber" + k, attribute.getAttributeType().getIsNumber());
                jsontranservcetype.put("attributetype" + k, tstA);
                k++;
                jsonAttributesArr.put(jsontranservcetype);
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        System.out.println("arraysizeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + jsonAttributesArr.length());
        return jsonAttributesArr;

    }

    @Path("/viewtranservicetype")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public JSONArray getTranServiceTypes() {
        JSONArray TransServJsonArr = new JSONArray();

        List<TranServiceType> TSTArr = tsth.loadAll();
        JSONObject jsontranservcetype = new JSONObject();
        System.out.println("TSTArrDataBaseeeee" + TSTArr.size());
        for (int i = 0; i < TSTArr.size(); i++) {
            JSONObject tst = new JSONObject();
            TranServiceType tstObject = TSTArr.get(i);
            try {
                tst.put("trantypename" + i, tstObject.getName());
                tst.put("trantypeid" + i, tstObject.getId());
                jsontranservcetype.put("trantype" + i, tst);
                TransServJsonArr.put(jsontranservcetype);
            } catch (JSONException ex) {
                ex.printStackTrace();
            }

        }

        return TransServJsonArr;
    }

    @Path("/add")
    //@Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    //  @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    @POST
    //  public String addTranServiceType(JSONArray TransServJsonArr) {
    public String addTranServiceType(JSONArray TSTAArr) {
        System.out.println("da5allllllll aladdddddddddddddddddddddddddddddddd");
        try {
            JSONObject jSONObject1 = TSTAArr.getJSONObject(0);
            String name = jSONObject1.getString("tsname");
            System.out.println("nameeeeeeee" + name);
            //  int TranServiceTypeID = 111;  ////////////////////////////////////////hytsal7 bsequence

            TranServiceType tst = new TranServiceType();
            tst.setName(name);
            //tst.setId(TranServiceTypeID); //mesh hab3to da sequence
            tsth.create(tst);

            for (int i = 1; i < TSTAArr.length(); i++) {
                JSONObject jSONObject = TSTAArr.getJSONObject(i);
                JSONObject att = jSONObject.getJSONObject("attributes");
                TranServiceTypeAttribute TSTAObject = new TranServiceTypeAttribute();
                TranServiceTypeAttributeId TSTAID = new TranServiceTypeAttributeId();
                System.out.println("iddddddddd" + att.getInt("attid"));
                TSTAID.setIdAttributeType(att.getInt("attid"));
                TSTAObject.setId(TSTAID);
                System.out.println("valueee" + att.getString("attvalue"));
                TSTAObject.setValue(att.getString("attvalue"));
                TSTAObject.getId().setIdTranServiceType(tst.getId());

                transTypeAttDAO.create(TSTAObject);
            }

        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        return "done";
    }

    @Path("/updatee")
    //@Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    //  @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    @POST
    //  public String addTranServiceType(JSONArray TransServJsonArr) {
    public String UpdateAttribute1(JSONArray TSTAArr) {
        try {
            JSONObject jSONObject1 = TSTAArr.getJSONObject(0);
            int tranid = jSONObject1.getInt("transid");
            System.out.println("transid" + tranid);

            TranServiceTypeAttributeId m = new TranServiceTypeAttributeId();
            m.setIdTranServiceType(tranid);
            TranServiceTypeAttributeHome dao = new TranServiceTypeAttributeHome(HibernateUtil.getSessionFactory());
            List<TranServiceTypeAttribute> oldTSTArr = dao.loadAll();
            for (int i = 0; i < oldTSTArr.size(); i++) {
                TranServiceTypeAttribute TSTAObject1 = oldTSTArr.get(i);
                if (TSTAObject1.getTranServiceType().getId() == tranid) {
                    dao.delete(TSTAObject1);
                }
            }

            for (int i = 1; i < TSTAArr.length(); i++) {
                JSONObject jSONObject;
                jSONObject = TSTAArr.getJSONObject(i);
                JSONObject att = jSONObject.getJSONObject("attributes");
                TranServiceTypeAttribute TSTAObject = new TranServiceTypeAttribute();
                TranServiceTypeAttributeId TSTAID = new TranServiceTypeAttributeId();
                System.out.println("iddddddddd" + att.getInt("attid"));
                TSTAID.setIdAttributeType(att.getInt("attid"));
                TSTAObject.setId(TSTAID);
                System.out.println("valueee" + att.getString("attvalue"));
                TSTAObject.setValue(att.getString("attvalue"));
                TSTAObject.getId().setIdTranServiceType(tranid);
                transTypeAttDAO.create(TSTAObject);
            }
        } catch (JSONException ex) {
            Logger.getLogger(TransportationServiceTypeService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/update/{oldid}/{tranid}/{name}/{valuee}")
    public String UpdateAttribute(@PathParam(value = "oldid") int oldid, @PathParam(value = "tranid") int tranid, @PathParam(value = "name") String name, @PathParam(value = "valuee") String value) {
        AttributeTypeHome dao1 = new AttributeTypeHome(HibernateUtil.getSessionFactory());
        List<AttributeType> result = dao1.loadAll();
        System.out.println("nameeeeeeeeeeeeeeeeee" + name + " valueeeeee " + value);
        int attid = 0;
        for (int i = 0; i < result.size(); i++) {
            AttributeType att = result.get(i);
            String attname = att.getName();
            if (attname.equals(name)) {
                attid = att.getId();
                break;
            }
        }
        System.out.println("serviceeeeeee id  " + attid);

        TranServiceTypeAttributeId m = new TranServiceTypeAttributeId();
        m.setIdAttributeType(oldid);
        m.setIdTranServiceType(tranid);
        TranServiceTypeAttributeHome dao = new TranServiceTypeAttributeHome(HibernateUtil.getSessionFactory());
        TranServiceTypeAttribute TSTAObject1 = dao.read(m);
        dao.delete(TSTAObject1);
        ///////////////////////////////////////////NEW/////////////////////// 
        TranServiceTypeAttribute TSTAObject = new TranServiceTypeAttribute();
        TranServiceTypeAttributeId tstaid = new TranServiceTypeAttributeId();
        System.out.println("original valueee" + TSTAObject.getValue());
        TSTAObject.setValue(value);
        tstaid.setIdTranServiceType(tranid);
        tstaid.setIdAttributeType(attid);
        TSTAObject.setId(tstaid);
        // TSTAObject.getAttributeType().setName(name);
        dao.create(TSTAObject);

        return "done";
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/getattid/{name}")
    public int getAttributeID(@PathParam(value = "name") String name) {
        AttributeTypeHome dao = new AttributeTypeHome(HibernateUtil.getSessionFactory());
        List<AttributeType> result = dao.loadAll();
        int id = 0;
        for (int i = 0; i < result.size(); i++) {
            AttributeType att = result.get(i);
            String attname = att.getName();
            if (attname.equals(name)) {
                id = att.getId();
                break;
            }
        }

        return id;
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/getattname/{id}")
    public String getAttributeName(@PathParam(value = "id") int attid) {
        AttributeTypeHome dao = new AttributeTypeHome(HibernateUtil.getSessionFactory());
        AttributeType attObject = dao.read(attid);
        String name = attObject.getName();
        return name;
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/addTSTObject/{transid}/{attid}/{attvalue}")
    public String addTranServiceTypeObject(@PathParam(value = "transid") int tranid, @PathParam(value = "attid") int attid, @PathParam(value = "attvalue") String value) {
        TranServiceTypeAttribute TSTAObject = new TranServiceTypeAttribute();
        TranServiceTypeAttributeId TSTAID = new TranServiceTypeAttributeId();
        TSTAID.setIdAttributeType(attid);
        TSTAObject.setId(TSTAID);
        TSTAObject.setValue(value);
        TSTAObject.getId().setIdTranServiceType(tranid);
        transTypeAttDAO.create(TSTAObject);

        return "done";
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/editTransTypeName/{id}/{name}")
    public String setTransportationTypeName(@PathParam(value = "id") int tranid, @PathParam(value = "name") String newname) {
        try {
            TranServiceTypeHome transServTypeDAO = new TranServiceTypeHome(HibernateUtil.getSessionFactory());
            TranServiceType tst = transServTypeDAO.read(tranid);
            tst.setName(newname);
            transServTypeDAO.update(tst);
            return "editnamedone";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "editnamenotdone";
        }
    }

    @GET
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/removets/{id}")
    public String removeTS(@PathParam(value = "id") int id)
    {
//         AttributeType attributeType = new AttributeType();
//        attributeType = dao.getObject(id);
//        dao.delete(attributeType);
     
        
        TranServiceTypeHome dao = new TranServiceTypeHome(HibernateUtil.getSessionFactory());
        TranServiceType TSTAObject1 = dao.read(id);
        dao.delete(TSTAObject1);
        return "done";
    }
    
    @GET
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/removetst/{attid}/{tsid}")
    public String removeTST(@PathParam(value = "attid") int attid,@PathParam(value = "tsid") int tsid)
    {
        TranServiceTypeAttributeHome tstadao = new TranServiceTypeAttributeHome(HibernateUtil.getSessionFactory());
        TranServiceTypeAttributeId id=new TranServiceTypeAttributeId();
        id.setIdAttributeType(attid);
        id.setIdTranServiceType(tsid);
        TranServiceTypeAttribute TSTAObject = tstadao.read(id);
        tstadao.delete(TSTAObject);
        return "done";
    }

    public static void main(String args[]) {
        TranServiceTypeHome dao = new TranServiceTypeHome(HibernateUtil.getSessionFactory());
        TranServiceType TSTAObject1 = dao.read(16);
        dao.delete(TSTAObject1);

    }
}
